<?php

namespace app\services;

use app\traits\Page;
use plugin\admin\app\model\Like;
use plugin\admin\app\model\Reward;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserFollowing;
use plugin\admin\app\model\UserProfile;
use plugin\admin\app\model\Video;
use plugin\admin\app\model\VideoComment;

class LikeService
{
    public static function beList($page, $length = 10)
    {
        $offset = ($page - 1) * $length;

        $query = Like::where('third_party_user_id', get_user_id());

        $totalCount = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->orderByDesc('id')->get()->toArray();

        $likeUserIds = array_column($list, 'user_id');
        $users =  UserProfile::select(['user_id', 'nickname', 'avatar', 'no'])->whereIn('user_id', $likeUserIds)->get()->toArray();
        $users = array_combine(
            array_column($users, 'user_id'),
            array_values($users)
        );

        $videoIds = [];
        $commentIds = [];

        foreach ($list as $item) {
            if ($item['type'] == Like::TYPE_VIDEO) {
                $videoIds[] = $item['third_party_id'];
            } else if ($item['type'] == Like::TYPE_COMMENT) {
                $commentIds[] = $item['third_party_id'];
            }
        }

        $videos = Video::select(['id', 'thumb', 'desc'])->whereIn('id', $videoIds)->get()->toArray();
        $videos = array_combine(
            array_column($videos, 'id'),
            array_values($videos)
        );

        $comments = VideoComment::select(['id', 'content'])->whereIn('id', $commentIds)->get()->toArray();
        $comments = array_combine(
            array_column($comments, 'id'),
            array_values($comments)
        );

        foreach ($list as $key=>$item) {
            $item['user_info'] = $users[$item['user_id']];

            if ($item['type'] == Like::TYPE_VIDEO) {
                $video = $videos[$item['third_party_id']];
                $video['thumb'] = full_url($video['thumb']);
                $item['video_info'] = $video;
            } else if ($item['type'] == Like::TYPE_COMMENT) {
                $item['comment_info'] = $comments[$item['third_party_id']];
            }

            $item['format_time'] = diff_time($item['created_time']);
            $list[$key] = $item;
        }

        return Page::wrap($totalCount, $page, $length, $list);
    }

    public static function videoList($userId, $page = 1, $length = 10)
    {
        $offset = ($page - 1) * $length;
        $query = Like::where('user_id', $userId)->where('type', Like::TYPE_VIDEO)->orderByDesc('created_time');

        $totalCount = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->get()->toArray();

        $videoIds = array_column($list, 'third_party_id');
        $videos = Video::select(['id', 'thumb', 'likes', 'money'])->whereIn('id', $videoIds)->get()->toArray();

        foreach ($videos as $key=>$item) {
            $videos[$key]['thumb'] = full_url($item['thumb']);
        }

        return Page::wrap($totalCount, $page, $length, $videos);
    }

    public static function like($thirdPartyId, $thirdPartyUserId, $type, callable $callback = null)
    {
        if (!self::isLike($thirdPartyId, $type)) {
            Like::insert([
                'user_id' => get_user_id(),
                'third_party_user_id' => $thirdPartyUserId,
                'third_party_id' => $thirdPartyId,
                'type' => $type,
                'created_time' => date('Y-m-d H:i:s')
            ]);
            if (!is_null($callback)) {
                $callback($thirdPartyId, $type);
            }
        }
    }

    public static function isLike($thirdPartyId, $type)
    {
        return Like::where('user_id', get_user_id())->where('third_party_id', $thirdPartyId)->where('type', $type)->count() > 0;
    }

    public static function cancelLike($thirdPartyId, $type, callable $callback = null)
    {
        if (self::isLike($thirdPartyId, $type)) {
            Like::where('user_id', get_user_id())->where('third_party_id', $thirdPartyId)->where('type', $type)->delete();
            if (!is_null($callback)) {
                $callback($thirdPartyId, $type);
            }
        }
    }

}